﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace Kyers._50kweb.Helper
{
   public static class ConvertObject
    {
       /// <summary>
       /// Convert Class To Object
       /// </summary>
       /// <param name="c"></param>
       /// <returns></returns>
       public static object ConvertToObject(object c)
       {

           object _c = c;
           return _c;
       }


       /// <summary>
       /// Convert Model To Entity
       /// </summary>
       /// <param name="_model"></param>
       /// <param name="_entity"></param>
       public static void ConvertModelToEntity(Object _model, ref Object _entity)
       {
           try
           {
               Type typemodel = _model.GetType();
               PropertyInfo[] propertiesmodel = typemodel.GetProperties();
               Type type_entity = _entity.GetType();
               PropertyInfo[] propertiesentity = type_entity.GetProperties();
               for (int i = 0; i < propertiesmodel.Length; i++)
                   for (int j = 0; j < propertiesentity.Length; j++)
                       if (propertiesmodel[i].Name == propertiesentity[j].Name)
                       {
                           var value = propertiesmodel[i].GetValue(_model, null);
                           propertiesentity[j].SetValue(_entity, value, null);
                       }
           }
           catch
           { }

       }
       /// <summary>
       /// Convert Entity To Model
       /// </summary>
       /// <param name="_model"></param>
       /// <param name="_entity"></param>
       public static void ConvertEntityToModel(Object _entity, ref Object _model)
       {
           try
           {
               Type typeentity = _entity.GetType();
               PropertyInfo[] propertiesentity = typeentity.GetProperties();
               Type type_model = _model.GetType();
               PropertyInfo[] propertiesmodel = type_model.GetProperties();
               for (int i = 0; i < propertiesentity.Length; i++)
               {
                   try
                   {
                       for (int j = 0; j < propertiesmodel.Length; j++)
                           if (propertiesmodel[j].Name == propertiesentity[i].Name)
                           {
                               var value = propertiesentity[i].GetValue(_entity, null);
                               propertiesmodel[j].SetValue(_model, value, null);
                               break;
                           }
                   }
                   catch { }
               }
           }
           catch
           { }

       }


    }
}
